﻿using System;
using System.IO;

namespace PefStudio.Services
{
    public class TextWriterLoggingService : ILoggingService
    {
        public TextWriter Writer { get; set; }

        public TextWriterLoggingService()
        {
            Writer = Console.Out;

            IsFatalEnabled = true;
            IsErrorEnabled = true;
            IsWarnEnabled = true;
            IsInfoEnabled = true;
            IsDebugEnabled = true;
        }

        public TextWriterLoggingService(TextWriter writer)
        {
            Writer = writer;
        }

        public bool IsDebugEnabled { get; set; }
        public bool IsInfoEnabled { get; set; }
        public bool IsWarnEnabled { get; set; }
        public bool IsErrorEnabled { get; set; }
        public bool IsFatalEnabled { get; set; }

        protected void Write(object message, Exception exception)
        {
            if (message != null)
            {
                Writer.WriteLine(message.ToString());
            }
            if (exception != null)
            {
                Writer.WriteLine(exception.ToString());
            }
        }

        public void Debug(object message)
        {
            if (IsDebugEnabled)
            {
                Write(message, null);
            }
        }

        public void DebugFormatted(string format, params object[] args)
        {
            Debug(string.Format(format, args));
        }

        public void Info(object message)
        {
            if (IsInfoEnabled)
            {
                Write(message, null);
            }
        }

        public void InfoFormatted(string format, params object[] args)
        {
            Info(string.Format(format, args));
        }

        public void Warn(object message)
        {
            Warn(message, null);
        }

        public void Warn(object message, Exception exception)
        {
            if (IsWarnEnabled)
            {
                Write(message, exception);
            }
        }

        public void WarnFormatted(string format, params object[] args)
        {
            Warn(string.Format(format, args));
        }

        public void Error(object message)
        {
            Error(message, null);
        }

        public void Error(object message, Exception exception)
        {
            if (IsErrorEnabled)
            {
                Write(message, exception);
            }
        }

        public void ErrorFormatted(string format, params object[] args)
        {
            Error(string.Format(format, args));
        }

        public void Fatal(object message)
        {
            Fatal(message, null);
        }

        public void Fatal(object message, Exception exception)
        {
            if (IsFatalEnabled)
            {
                Write(message, exception);
            }
        }

        public void FatalFormatted(string format, params object[] args)
        {
            Fatal(string.Format(format, args));
        }
    }
}
